<template>
	<view class="scanQR-container">
		<view class="device-info" v-if="detailsInfo">
			<view class="title">设备信息</view>
			<view class="info-item">
				<text class="lable">设备ID：</text>
				<text class="value">{{detailsInfo.device_id||'-'}}</text>
			</view>
			<view class="info-item">
				<text class="lable">设备状态：</text>
				<text class="value">{{detailsInfo.info.DeviceStatus==1?'离线':'在线'||'-'}}</text>
			</view>
			<view class="info-item">
				<text class="lable">网络状态：</text>
				<text class="value">{{detailsInfo.info.DeviceNetStatus==1?'4G在线':detailsInfo.info.DeviceNetStatus==2?'wifi在线':'-'}}</text>
			</view>
			<view class="info-item">
				<text class="lable">电压：</text>
				<text class="value">{{detailsInfo.info.Vol}}V</text>
			</view>
			<view class="info-item">
				<text class="lable">总电流：</text>
				<text class="value">{{detailsInfo.info.Current}}A</text>
			</view>
			<view class="info-item">
				<text class="lable">温度：</text>
				<text class="value">{{detailsInfo.info.Temp}}℃</text>
			</view>
			<view class="info-item">
				<text class="lable">当前网络信号强度：</text>
				<text class="value">{{detailsInfo.info.Rssi}}</text>
			</view>
			<view class="info-item" v-if="detailsInfo.info&&detailsInfo.info.DeviceAlarm">
				<text class="lable">设备告警信息：</text>
				<text class="value">{{formatAlarm(detailsInfo.info.DeviceAlarm)}}</text>
			</view>
			<view class="info-item">
				<text class="lable">激活状态：</text>
				<text class="value">{{detailsInfo.status==1?'已激活':'未激活'}}</text>
			</view>
		</view>
		
		<view class="activate-btn" v-if="detailsInfo" @tap="handleActivate">
			<text>立即激活</text>
		</view>
		
		<view class="bottom-box" @tap="scanQRCode">
			<view class="scan-btn">
				<text>点击此处扫描盒子内二维码</text>
				<uni-icons type="scan" size="20" color="#fff"></uni-icons>
			</view>
		</view>
	</view>
</template>

<script>
	export default{
		data(){
			return{
				deviceId:'',
				detailsInfo:'',
			}
		},
		onLoad() {
			// this.getDeviceInfo()
		},
		methods:{
			scanQRCode(){
				uni.scanCode({
					success: res=> {
						console.log('条码类型：' + res.scanType);
						console.log('条码内容：' + res.result);
						this.deviceId=res.result
						this.getDeviceInfo()
					}
				});
			},
			
			// 根据设备id获取设备信息
			getDeviceInfo(){
				this.$request.post(this.$api.smartDeviceInfo, {
					device_id:this.deviceId
				}, {
					token:true
				}).then(res => {
					let {
						data = {}
					} = res;
					if (res.code==1) {
						// console.log(data,"设备信息")
						this.detailsInfo=data
					}
				})
			},
			
			// 激活设备
			handleActivate(){
				this.$request.post(this.$api.activateDevice, {
					device_id:this.deviceId,
					store_id:uni.getStorageSync('store_id')
				}, {
					token:true
				}).then(res => {
					let {
						data = {}
					} = res;
					setTimeout(()=>{
						uni.navigateBack()
					},1000)
				})
			},
			
			formatAlarm(value){
				switch (value) {
				    case '0x01':
				      return '未接地故障';
				    case '0x02':
				      return '过压故障';
				    case '0x04':
				      return '欠压故障';
				    case '0x08':
				      return '零火故障';
					case '0x10':
					  return '温度过高';
					case '0x20':
					  return '总功率过高';
				}
			}
		}
	}
</script>

<style lang="scss" acoped>
	.scanQR-container{
		padding: 40rpx;
		.img-box{
			width: 100%;
			height: 380rpx;
			border-radius: 16rpx;
			overflow: hidden;
		}
		.device-info{
			color:#fff;
			.title{
				text-align: center;
				font-size: 30rpx;
				font-weight: bold;
				margin-bottom: 20rpx;
			}
			.info-item{
				margin-bottom: 10rpx;
				.lable{
					font-size: 30rpx;
					color:rgba(255, 255, 255, 0.9);
				}
				.value{
					font-size: 32rpx;
					color:rgba(255, 255, 255, 1);
				}
			}
		}
		.activate-btn{
			display: flex;
			justify-content: center;
			margin-top:80rpx;
			>text{
				width:160rpx;
				height: 64rpx;
				line-height: 64rpx;
				padding: 0 20rpx;
				box-sizing: border-box;
				text-align: center;
				color: #fff;
				font-size: 28rpx;
				background: rgba(86, 119, 252, 1);
				border-radius: 8rpx;
			}
		}
		.bottom-box{
			width: 100%;
			position: absolute;
			bottom: 88rpx;
			left: 0;
			.scan-btn{
				width: 670rpx;
				height: 80rpx;
				line-height: 80rpx;
				margin: 0 auto;
				background: rgb(31, 124, 247);
				border-radius: 72rpx;
				display: flex;
				justify-content: center;
				align-items: center;
				gap:10rpx;
				text{
					font-size: 32rpx;
					color: #fff;
					margin-bottom: 6rpx;
				}
			}
		}
	}
</style>